;Exercise 1.18
;Using the results of Exercise 1.16 and Exercise 1.17, devise a procedure that
;generates an iterative process for multiplying two integers in terms of 
;adding, doubling, and halving and uses a logarithmic number of steps.

(define (fmx a b)
  (m a b 0))

(define (m a b x)
  (cond ((= b 0) x)
        ((even? b) (m (double a) (halve b) x))
        (else (m a (- b 1) (+ x a)))))

(define (even? n)
  (= (remainder n 2) 0))

(define (double n)
  (* n 2))

(define (halve n)
  (/ n 2))
